package ungdung_quanlymathang;
import java.util.List;

import com.example.quanly_mathang.R;


import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class Adapter_customlist extends ArrayAdapter<mat_hang> {
	private final List<mat_hang> list;
    private final Activity context;
    boolean checkAll_flag = false;
    boolean checkItem_flag = false;
    
    public Adapter_customlist(Activity context, List<mat_hang> list) {
        super(context, R.layout.custom_listview, list);
        this.context = context;
        this.list = list;
    }
    
    static class ViewHolder {
        protected TextView text;
        protected TextView text2;
        protected TextView text3;
        protected TextView text4;
        protected CheckBox checkbox;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
        ViewHolder viewHolder = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            convertView = inflator.inflate(R.layout.custom_listview, null);
            viewHolder = new ViewHolder();
            viewHolder.text = (TextView) convertView.findViewById(R.id.textView1);
            viewHolder.text2 = (TextView) convertView.findViewById(R.id.textView3);
            viewHolder.text3 = (TextView) convertView.findViewById(R.id.textView2);
            viewHolder.text4 = (TextView) convertView.findViewById(R.id.textView4);
            viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);
            viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int getPosition = (Integer) buttonView.getTag();  
            list.get(getPosition).setSelected(buttonView.isChecked());                 }
            });
            convertView.setTag(viewHolder);
            convertView.setTag(R.id.textView1, viewHolder.text);
            convertView.setTag(R.id.textView3, viewHolder.text2);
            convertView.setTag(R.id.textView2, viewHolder.text3);
            convertView.setTag(R.id.textView4, viewHolder.text4);
            convertView.setTag(R.id.checkBox1, viewHolder.checkbox);
            } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.checkbox.setTag(position); 
        
        viewHolder.text.setText(list.get(position).get_ten_mathang());
        viewHolder.text2.setText(list.get(position).get_gia_mathang());
        viewHolder.text3.setText(list.get(position).get_ngay_capnhat());
        viewHolder.text4.setText(list.get(position).get_nha_sanxuat());
        viewHolder.checkbox.setChecked(list.get(position).isSelected());
        
        return convertView;
    }
}


